const AVLTree = require("../template")

function testAVLTreeBasic() {
  const avl = new AVLTree()

  console.log(
    "1. 基本插入测试:",
    (() => {
      avl.insert(10)
      avl.insert(20)
      avl.insert(30)
      avl.insert(40)
      avl.insert(50)
      return avl.search(10) !== null &&
             avl.search(20) !== null &&
             avl.search(30) !== null &&
             avl.search(40) !== null &&
             avl.search(50) !== null
    })()
  )

  console.log(
    "2. 基本搜索测试:",
    (() => {
      return avl.search(10) !== null &&
             avl.search(20) !== null &&
             avl.search(60) === null &&
             avl.search(5) === null
    })()
  )

  console.log(
    "3. AVL树性质测试:",
    (() => {
      return avl.isValid() === true
    })()
  )

  console.log(
    "4. 中序遍历测试:",
    (() => {
      const result = avl.inorder()
      const expected = [10, 20, 30, 40, 50]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 最小值和最大值测试:",
    (() => {
      return avl.getMin() === 10 &&
             avl.getMax() === 50
    })()
  )

  console.log(
    "6. 基本删除测试:",
    (() => {
      avl.delete(30)
      return avl.search(30) === null &&
             avl.search(10) !== null &&
             avl.search(20) !== null &&
             avl.search(40) !== null &&
             avl.search(50) !== null
    })()
  )

  console.log(
    "7. this上下文AVL树测试:",
    (() => {
      const avlObj = {
        multiplier: 2,
        processAVLTree: function(values) {
          const avl = new AVLTree()
          values.forEach(val => avl.insert(val))
          return avl.getSize() * this.multiplier
        }
      }
      return avlObj.processAVLTree([1, 2, 3]) === 6 // 3*2=6
    })()
  )

  console.log(
    "8. 复杂对象this绑定AVL树测试:",
    (() => {
      const avlConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(values) {
          const avl = new AVLTree()
          values.forEach(val => avl.insert(val))
          return avl.getSize() >= this.config.threshold
        }
      }
      return avlConfigObj.processWithConfig([1, 2, 3]) === true
    })()
  )

  console.log(
    "9. AVL树一致性测试:",
    (() => {
      const consistencyAvl = new AVLTree()
      consistencyAvl.insert(5)
      consistencyAvl.insert(3)
      consistencyAvl.insert(7)
      const size1 = consistencyAvl.getSize()
      consistencyAvl.delete(3)
      const size2 = consistencyAvl.getSize()
      return size1 === 3 && size2 === 2
    })()
  )

  console.log(
    "10. AVL树边界测试:",
    (() => {
      const boundaryAvl = new AVLTree()
      boundaryAvl.insert(1)
      return boundaryAvl.getMin() === 1 &&
             boundaryAvl.getMax() === 1 &&
             boundaryAvl.getSize() === 1
    })()
  )

  console.log(
    "11. AVL树性能测试:",
    (() => {
      const perfAvl = new AVLTree()
      const values = Array.from({ length: 100 }, (_, i) => i + 1)
      const start = performance.now()
      values.forEach(val => perfAvl.insert(val))
      values.forEach(val => perfAvl.search(val))
      const end = performance.now()
      return perfAvl.getSize() === 100 && (end - start) < 1000
    })()
  )

  console.log(
    "12. AVL树复杂测试:",
    (() => {
      const complexAvl = new AVLTree()
      const values = [50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85]
      values.forEach(val => complexAvl.insert(val))
      const isValid = complexAvl.isValid()
      const inorder = complexAvl.inorder()
      const min = complexAvl.getMin()
      const max = complexAvl.getMax()
      return isValid === true &&
             inorder.length === 15 &&
             min === 10 &&
             max === 85
    })()
  )
}

testAVLTreeBasic()
